window.Slider=function(options){
	var data=options.data||[];								//数据
	var width=options.width;								//宽度
	var index=0;											//当前位置
	var count=data.length;									//个数
	var easing=options.easing||0.6;							//缓动系数
	var speed=options.speed||100;							//移动速度
	var time=options.time||1000;							//时间间隔
	var timer;												//定时器
	var run;												//移动器
	var room=getDom(options.room);							//li容器
	var btn=getDom(options.btn);							//按钮容器
	//节点生成
	data.forEach(function(obj,i,arr){
		var boxLi=createBoxli(obj.pic);
		var btnli=createDom("li");
		btnli["data-i"]=i;
		room.appendChild(boxLi);
		btn.appendChild(btnli);
	});
	room.style.width=width*count+"px";
	room.style.left=0+"px";
	btn.style.width=20*count+10*count+"px";
	btn.style.left=(width-(20*count+10*count))/2+"px";
	btn.firstElementChild.className="active";
	timingMove();//初始运动
	// 按钮点击
	btn.addEventListener("click",function(e){
		var dom=e.target;
		var parent=dom.parentNode;
		if(parent.id==="btn"&&dom.nodeName==="LI"){
			clearInterval(timer);
			index=dom["data-i"];
			timingMove();
		}
	});
	// 移动位置
	function movePlace(){
		clearInterval(run);
		var dis=index*width;
		var curDis=Math.abs(room.style.left.replace("px",""));
		run=setInterval(function(){
			if(dis!==curDis){
				if(dis>curDis){
					curDis+=Math.ceil((dis-curDis)*easing);
				}else{
					curDis-=Math.ceil((curDis-dis)*easing);
				}
				room.style.left=-curDis+"px";
			}else{
				clearInterval(run);
				timer=setTimeout(timingMove,time);
			}
		},speed);
	}
	// 定时滚动
	function timingMove(){
		if(index>=count){
			index=0;
		}
		movePlace(index);
		activeChange(index);
		index++;
	}
	//活动状态改变
	function activeChange(id){
		var btnArrs=Array.prototype.slice.call(btn.childNodes);
		btnArrs.forEach(function(obj,i,arr){
			if(obj.nodeType=="1"){
				obj["data-i"]===id?obj.className="active":obj.className="";
			}
		});
	}
	//创建box-child
	function createBoxli(pic){
		var img=createDom("img");
		var li=createDom("li");
		img.src="images/"+pic;
		li.appendChild(img);
		return li;
	}
	//获取dom
	function getDom(name){
		return document.querySelectorAll(name)[0];
	}
	//创建dom
	function createDom(type){
		return document.createElement(type);
	}
}

var data=[{"pic":"bj01.png"},{"pic":"bj02.png"},{"pic":"bj03.png"},{"pic":"bj04.png"}];//模拟数据
Slider({
	"data":data,
	"width":720,
	"btn":"#btn",
	"room":"#box",
	"time":1000,
	"easing":0.6,
	"speed":100
})